package com.bps.zzant.controller;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController extends ZzantController {
	
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	
	/**
	 * Simply selects the home view to render by returning its name.
	 */

	
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String apphome(Locale locale, Model model, HttpServletRequest request) {
		logger.info("Welcome home! the client locale is "+ locale.toString());
		logger.info("getRequestURI():" + request.getRequestURI());
		
		logger.info("getServletPath():" + request.getServletPath());
		logger.info("getPathInfo():" + request.getPathInfo());
		
		logger.info("getServerName():" + request.getServerName());
		logger.info("getServerPort():" + request.getServerPort());		
		logger.info("getServletPath():" + request.getServletPath());
		
		String logoUrl = "http://" + request.getServerName()+":"+request.getServerPort()+request.getRequestURI()+"resources/theme/images/big_logo.jpg";
		 logger.info("logoUrl:" + logoUrl);
        
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
		
		String formattedDate = dateFormat.format(date);
		
		Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		HttpSession session = request.getSession(true); //create a new session

			// put the UserDetails object here.
		session.setAttribute("userDetails", principal);
		
		
		model.addAttribute("serverTime", formattedDate );
		
		return "apphome";
	}
}
